package com.yzjs.localhost.service.hrmProfessionalQualification.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmEducationInfo.HrmEducationInfoEntity;
import com.yzjs.localhost.entity.hrmFamilyMembers.HrmFamilyMembersEntity;
import com.yzjs.localhost.entity.hrmProfessionalQualification.HrmProfessionalQualificationEntity;
import com.yzjs.localhost.dao.hrmProfessionalQualification.HrmProfessionalQualificationMapper;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmEducationInfo.impl.HrmEducationInfoServiceImpl;
import com.yzjs.localhost.service.hrmProfessionalQualification.HrmProfessionalQualificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("hrmProfessionalQualificationService")
public class HrmProfessionalQualificationServiceImpl extends BaseServiceImpl<HrmProfessionalQualificationEntity, HrmProfessionalQualificationMapper > implements HrmProfessionalQualificationService {

    @Override
    public ComparisonResultDTO hrmProfessionalQualificationCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity before = dto.getBeforce();
        String beforeInfo = before.getInfo();
        Map<String, Object> beforeParse = JsonUtil.json2map(beforeInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();
        ComparisonResultDTO resultDTO = null;
        Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
        if (before.getOperatorCode().contains("qualification:add")) {
            // 比对 执业资格状态,变动后执业资格证书的名称，原颁发机构，变动后颁发机构，原执业资格证书的名称，变动原因，原机构所在地，变动后机构所在地，用户ID，创建人，变动时间
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse,
                    "quaStatus,quaBdmc,quaYjg,quaBdjg,quaYmc,quaReason,quaYszd,quaBdszd,quaUser,createBy,quaDate");
        } else if (before.getOperatorCode().contains("qualification:edit")) {
            /* 比对 部门信息，执业资格状态，变动后执业资格证书的名称，原颁发机构，员工编号，删除标记，更新人，主键ID，创建时间，变动后颁发机构，
             原执业资格证书的名称，变动原因，员工姓名，原机构所在地，变动后机构所在地，用户ID，创建时间，变动时间*/
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse,
                    "deptName,quaStatus,quaBdmc,quaYjg,staffNum,deleteFlag,updateBy,quaId,createDate,quaBdjg,quaYmc,quaReason,fullName,quaYszd,quaBdszd,quaUser,createBy,quaDate");
        } else if (before.getOperatorCode().contains("qualification:remove")) {
            beforeParse.put("delFlag", "2");
            // 比对 主键ID
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "quaIds,delFlag");
            if (null==afterParse.get("quaIds")){
                resultDTO.setAbstractName("主键ID为空");
                resultDTO.setWarningInfo("主键ID为空");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            HrmProfessionalQualificationEntity hrmProfessionalQualificationEntity = new HrmProfessionalQualificationEntity();
            hrmProfessionalQualificationEntity.setQuaId(Long.valueOf((String) afterParse.get("quaIds")));
            hrmProfessionalQualificationEntity.setDeleteFlag((String) afterParse.get("delFlag"));
            this.insert(hrmProfessionalQualificationEntity);
            return resultDTO;
        }
        // 记录表信息
        HrmProfessionalQualificationEntity hrmProfessionalQualificationEntity = JSONObject.parseObject(after.getInfo(), HrmProfessionalQualificationEntity.class);
        hrmProfessionalQualificationEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        this.insert(hrmProfessionalQualificationEntity);
        return resultDTO;
    }

}
